package com.ciaojian.core.util;

import cn.hutool.core.util.StrUtil;
import com.ciaojian.core.factory.ObjectMapperFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Atlakyn
 */
public class LogUtil {
    /**
     * 前缀
     */
    private static final String PREFIX = " - ";

    private final static Logger PAY_LOG = LoggerFactory.getLogger("PAY_LOG");
    private final static Logger DATABASE_LOG = LoggerFactory.getLogger("DATABASE_LOG");
    private final static Logger EXCEPTION_LOG = LoggerFactory.getLogger("EXCEPTION_LOG");
    private final static Logger BUSINESS_LOG = LoggerFactory.getLogger("BUSINESS_LOG");
    private final static Logger PLATFORM_LOG = LoggerFactory.getLogger("PLATFORM_LOG");
    private final static Logger SCHEDULE_LOG = LoggerFactory.getLogger("SCHEDULE_LOG");

    public static void writePayLog(String content, Object... objects) {
        PAY_LOG.info(PREFIX.concat(content), objects);
    }

    /**
     * 写日志
     *
     * @param content 日志内容
     */
    public static void writeBusinessLog(String content, Object... objects) {
        BUSINESS_LOG.info(PREFIX.concat(content), objects);
    }

    /**
     * 业务异常日志
     *
     * @param content 日志内容
     */
    public static void writeBusinessErrorLog(String content, Object... objects) {
        BUSINESS_LOG.error(PREFIX.concat(content), objects);
    }

    /**
     * 业务异常日志
     *
     * @param content 日志内容
     */
    public static void writeBusinessErrorLog(Throwable throwable, String content, Object... objects) {
        StackTraceElement stackTraceElement = throwable.getStackTrace()[0];
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        EXCEPTION_LOG.error("****************************************************************************************************************************************************************************");
        EXCEPTION_LOG.error("请求接口={}", request.getRequestURL());
        EXCEPTION_LOG.error("异常定位={}", stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "#" + stackTraceElement.getLineNumber());
        EXCEPTION_LOG.error("错误信息=" + (StrUtil.isBlank(content) ? "{}" : content), objects);
        Map<String, Object> map = new LinkedHashMap<>();
        ObjectMapper om = ObjectMapperFactory.getObjectMapperInstance();
        Enumeration<String> pNames = request.getParameterNames();
        while (pNames.hasMoreElements()) {
            String name = pNames.nextElement();
            String value = request.getParameter(name);
            map.put(name, value);
        }

        try {
            EXCEPTION_LOG.error("请求参数={}", "".equals(om.writeValueAsString(map)) ? "{}" : om.writeValueAsString(map));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            EXCEPTION_LOG.error("JSON格式化异常={}", map);
        }

        EXCEPTION_LOG.error("", throwable);
    }


    /**
     * 数据库错误日志
     *
     * @param content 日志内容
     */
    public static void writeDbErrorLog(String content, Object... objects) {
        DATABASE_LOG.error(PREFIX.concat(content), objects);
    }

    /**
     * 写错误日志
     *
     * @param content 日志内容
     */
    public static void writeErrorLog(String content, Object... objects) {
        EXCEPTION_LOG.error(PREFIX.concat(content), objects);
    }

    /**
     * 写错误日志
     *
     * @param throwable 异常对象
     * @param content   日志内容
     * @param objects   参数
     */
    public static void writeErrorLog(Throwable throwable, String content, Object... objects) {
        StackTraceElement stackTraceElement = throwable.getStackTrace()[0];
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        EXCEPTION_LOG.error("****************************************************************************************************************************************************************************");
        EXCEPTION_LOG.error("请求接口={}", request.getRequestURL());
        EXCEPTION_LOG.error("异常定位={}", stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "#" + stackTraceElement.getLineNumber());
        EXCEPTION_LOG.error("错误信息=" + (StrUtil.isBlank(content) ? "{}" : content), objects);
        Map<String, Object> map = new LinkedHashMap<>();
        ObjectMapper om = ObjectMapperFactory.getObjectMapperInstance();
        Enumeration<String> pNames = request.getParameterNames();
        while (pNames.hasMoreElements()) {
            String name = pNames.nextElement();
            String value = request.getParameter(name);
            map.put(name, value);
        }

        try {
            EXCEPTION_LOG.error("请求参数={}", "".equals(om.writeValueAsString(map)) ? "{}" : om.writeValueAsString(map));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            EXCEPTION_LOG.error("JSON格式化异常={}", map);
        }

        EXCEPTION_LOG.error("", throwable);
    }

    /**
     * 写定时任务信息
     *
     * @param content 日志内容
     * @param objects 参数
     */
    public static void writeScheduleLog(String content, Object... objects) {
        SCHEDULE_LOG.info(PREFIX.concat(content), objects);
    }

    /**
     * 写信息
     *
     * @param msg 信息
     */
    private static void writeMessage(String msg) {

    }

}
